/*******************************************************************************
 * Copyright (c) 2013 INTERAMERICAN PROPERTY AND CASUALTY INSURANCE COMPANY S.A. 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Lesser General Public License for more details.
 ******************************************************************************/
package gr.interamerican.bo2.arch.utils.comparators;

import gr.interamerican.bo2.arch.PersistentObject;
import gr.interamerican.bo2.utils.Utils;

import java.io.Serializable;
import java.util.Comparator;


/**
 * Compares {@link PersistentObject}s.
 * 
 * @param <K>
 *        Type of Key 
 * @param <P>
 *        Type of PersistentObject
 */
public class PoComparator
<K extends Serializable & Comparable<? super K>, 
 P extends PersistentObject<K>> 
implements Comparator<P> {

	public int compare(P left, P right) {
		if (left==null && right==null) {
			return 0;
		}
		if (left==null) {
			return -1;
		}
		if (right==null) {
			return 1;
		}
		
		return Utils.nullSafeCompare(left.getKey(), right.getKey());
	}

}
